#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
  FILE *f_bin, *f_operate;
  uint8_t *pdata;
  int data_size;
  unsigned int mark;

  if( argc == 2 ) {
    printf("The argument supplied is %s\n", argv[1]);
    sscanf(argv[1], "%X", &mark);
  }
  else if( argc > 2 ) {
    printf("Too many arguments supplied.\n");
    return 0;
  }
  else {
    printf("One argument expected.\n");
    return 0;
  }
  f_operate = fopen("xldz_bootloader.bin", "wb");
  f_bin = fopen("build/bootloader.bin", "rb");
  fseek(f_bin, 0, SEEK_END);
  data_size = ftell(f_bin);
  fseek(f_bin, 0, SEEK_SET);
  pdata = (uint8_t*)malloc(data_size);
  fread(pdata, data_size, 1, f_bin);
  fwrite(pdata, data_size, 1, f_operate);
  mark &= 0x0000FFFF;
  mark |= 0x55550000;
  fwrite(&mark, 4, 1, f_operate);
  fflush(f_operate);
  free(pdata);
  fclose(f_bin);
  fclose(f_operate);
  printf("done.\n");
  return 0;
}
